using MediatR;
using Medical.Domain;
using Medical.Infrastructrue.Interfaces;
using Medical.ReadApi.Dto;
using Medical.WriteApi.Appcontext.Command;
using Medical.WriteApi.Dto;
using Yitter.IdGenerator;

namespace Medical.WriteApi.Appcontext.CommandHandler
{
	public class CreateEmployeeCommandHandler : IRequestHandler<CreateEmployeeCommand, EmployeeDto>
	{
		private readonly IBaseRepository<Employee> _repository;

		public CreateEmployeeCommandHandler(IBaseRepository<Employee> repository)
		{
			_repository = repository;
		}

		public async Task<EmployeeDto> Handle(CreateEmployeeCommand request, CancellationToken cancellationToken)
		{
			var entity = new Employee
			{
				EmployeeId = YitIdHelper.NextId(),
				UserName = request.UserName,
				FullName = request.FullName,
				WorkNo = request.WorkNo,
				DepartmentId = request.DepartmentId,
				Email = request.Email,
				Gender = request.Gender,
				Address = request.Address,
				Phone = request.Phone,
				IdCardNo = request.IdCardNo,
				Title = request.Title,
				Role = request.Role,
				IsActive = request.IsActive,
				CreatedAt = DateTime.Now,
				UpdatedAt = DateTime.Now
			};

			await _repository.InsertAsync(entity);

			return new EmployeeDto
			{
				EmployeeId = entity.EmployeeId,
				UserName = entity.UserName,
				FullName = entity.FullName,
				WorkNo = entity.WorkNo,
				DepartmentId = entity.DepartmentId,
				Email = entity.Email,
				Gender = entity.Gender,
				Address = entity.Address,
				Phone = entity.Phone,
				IdCardNo = entity.IdCardNo,
				Title = entity.Title,
				Role = entity.Role,
				IsActive = entity.IsActive,
				CreatedAt = entity.CreatedAt,
				UpdatedAt = entity.UpdatedAt
			};
		}
	}
}
